//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FraRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Double rate, Cephei::QL::Times::IPeriod^ periodToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CFraRateHelper::typeid)
{
    CPeriod^ _CperiodToStart;
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        _CperiodToStart = safe_cast<CPeriod^> (periodToStart);
        _CperiodToStart->Lock();
        QuantLib::Period _periodToStart = static_cast<QuantLib::Period> (_CperiodToStart->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _periodToStart,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CperiodToStart != nullptr) _CperiodToStart->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ periodToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CFraRateHelper::typeid)
{
    CQuote^ _Crate;
    CPeriod^ _CperiodToStart;
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _CperiodToStart = safe_cast<CPeriod^> (periodToStart);
        _CperiodToStart->Lock();
        QuantLib::Period _periodToStart = static_cast<QuantLib::Period> (_CperiodToStart->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _periodToStart,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_CperiodToStart != nullptr) _CperiodToStart->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Double rate, Cephei::QL::Times::IPeriod^ periodToStart, UInt32 lengthInMonths, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CFraRateHelper::typeid)
{
    CPeriod^ _CperiodToStart;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        _CperiodToStart = safe_cast<CPeriod^> (periodToStart);
        _CperiodToStart->Lock();
        QuantLib::Period _periodToStart = static_cast<QuantLib::Period> (_CperiodToStart->GetReference ()); 
        QuantLib::Natural _lengthInMonths = (QuantLib::Natural)ValueHelper::Convert (lengthInMonths); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _periodToStart,  _lengthInMonths,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CperiodToStart != nullptr) _CperiodToStart->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ periodToStart, UInt32 lengthInMonths, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CFraRateHelper::typeid)
{
    CQuote^ _Crate;
    CPeriod^ _CperiodToStart;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _CperiodToStart = safe_cast<CPeriod^> (periodToStart);
        _CperiodToStart->Lock();
        QuantLib::Period _periodToStart = static_cast<QuantLib::Period> (_CperiodToStart->GetReference ()); 
        QuantLib::Natural _lengthInMonths = (QuantLib::Natural)ValueHelper::Convert (lengthInMonths); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _periodToStart,  _lengthInMonths,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_CperiodToStart != nullptr) _CperiodToStart->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Double rate, UInt32 monthsToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CFraRateHelper::typeid)
{
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        QuantLib::Natural _monthsToStart = (QuantLib::Natural)ValueHelper::Convert (monthsToStart); //d
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _monthsToStart,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Cephei::QL::IQuote^ rate, UInt32 monthsToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CFraRateHelper::typeid)
{
    CQuote^ _Crate;
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        QuantLib::Natural _monthsToStart = (QuantLib::Natural)ValueHelper::Convert (monthsToStart); //d
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _monthsToStart,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Double rate, UInt32 monthsToStart, UInt32 monthsToEnd, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CFraRateHelper::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        QuantLib::Natural _monthsToStart = (QuantLib::Natural)ValueHelper::Convert (monthsToStart); //d
        QuantLib::Natural _monthsToEnd = (QuantLib::Natural)ValueHelper::Convert (monthsToEnd); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _monthsToStart,  _monthsToEnd,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (Cephei::QL::IQuote^ rate, UInt32 monthsToStart, UInt32 monthsToEnd, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CFraRateHelper::typeid)
{
    CQuote^ _Crate;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFraRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        QuantLib::Natural _monthsToStart = (QuantLib::Natural)ValueHelper::Convert (monthsToStart); //d
        QuantLib::Natural _monthsToEnd = (QuantLib::Natural)ValueHelper::Convert (monthsToEnd); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper ( _rate,  _monthsToStart,  _monthsToEnd,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (boost::shared_ptr<QuantLib::FraRateHelper>& childNative, Object^ owner) : CRateHelper(CFraRateHelper::typeid)
{
#ifdef HANDLE
	_phFraRateHelper = NULL;
#endif
	_ppFraRateHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (QuantLib::FraRateHelper& childNative, Object^ owner) : CRateHelper(CFraRateHelper::typeid)
{
#ifdef HANDLE
	_phFraRateHelper = NULL;
#endif
	_ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
    _FraRateHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (CFraRateHelper^ copy) : CRateHelper(CFraRateHelper::typeid)
{
#ifdef HANDLE
	_phFraRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (PLATFORM::Type^ t) : CRateHelper(CFraRateHelper::typeid)
{
#ifdef HANDLE
	_phFraRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CFraRateHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (QuantLib::Handle<QuantLib::FraRateHelper>& childNative, Object^ owner)  : CRateHelper(CFraRateHelper::typeid)
{
	_phFraRateHelper = &childNative;
	_ppFraRateHelper = &static_cast<boost::shared_ptr<QuantLib::FraRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
    _FraRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (QuantLib::Handle<QuantLib::FraRateHelper> childNative)  : CRateHelper(CFraRateHelper::typeid)
{
	_phFraRateHelper = &childNative;
	_ppFraRateHelper = &static_cast<boost::shared_ptr<QuantLib::FraRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CFraRateHelper::CFraRateHelper (QuantLib::FraRateHelper childNative)  : CRateHelper(CFraRateHelper::typeid)
{
#ifdef HANDLE
	_phFraRateHelper = NULL;
#endif
	_ppFraRateHelper = new boost::shared_ptr<QuantLib::FraRateHelper> (new QuantLib::FraRateHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppFraRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CFraRateHelper::~CFraRateHelper ()
{
    if (_ppFraRateHelper != NULL)
    {
	    delete _ppFraRateHelper;
        _ppFraRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CFraRateHelper::!CFraRateHelper ()
{
    if (_ppFraRateHelper != NULL)
    {
	    delete _ppFraRateHelper;
    }
}
QuantLib::FraRateHelper& Cephei::QL::Termstructures::Yield::CFraRateHelper::GetReference ()
{
    if (_ppFraRateHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppFraRateHelper;
}
boost::shared_ptr<QuantLib::FraRateHelper>& Cephei::QL::Termstructures::Yield::CFraRateHelper::GetShared ()
{
    if (_ppFraRateHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppFraRateHelper;
}
QuantLib::FraRateHelper* Cephei::QL::Termstructures::Yield::CFraRateHelper::GetPointer ()
{
    if (_ppFraRateHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppFraRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FraRateHelper>& Cephei::QL::Termstructures::Yield::CFraRateHelper::GetHandle ()
{
	if (_phFraRateHelper == NULL)
	{
		_phFraRateHelper = new Handle<QuantLib::FraRateHelper> (*_ppFraRateHelper);
	}
	return *_phFraRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CFraRateHelper::HasNative () 
{
	return (_ppFraRateHelper != NULL);
}

Double Cephei::QL::Termstructures::Yield::CFraRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFraRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppFraRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Double rate, Cephei::QL::Times::IPeriod^ periodToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CFraRateHelper ( rate,  periodToStart,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ periodToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CFraRateHelper ( rate,  periodToStart,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Double rate, Cephei::QL::Times::IPeriod^ periodToStart, UInt32 lengthInMonths, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CFraRateHelper ( rate,  periodToStart,  lengthInMonths,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ periodToStart, UInt32 lengthInMonths, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CFraRateHelper ( rate,  periodToStart,  lengthInMonths,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Double rate, UInt32 monthsToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CFraRateHelper ( rate,  monthsToStart,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, UInt32 monthsToStart, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CFraRateHelper ( rate,  monthsToStart,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Double rate, UInt32 monthsToStart, UInt32 monthsToEnd, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CFraRateHelper ( rate,  monthsToStart,  monthsToEnd,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
Cephei::QL::Termstructures::Yield::IFraRateHelper^ Cephei::QL::Termstructures::Yield::CFraRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, UInt32 monthsToStart, UInt32 monthsToEnd, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CFraRateHelper ( rate,  monthsToStart,  monthsToEnd,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
